package com.situ.personal.controlle;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.personal.dao.DepartmentDao;
import com.situ.personal.dao.UserDao;
import com.situ.personal.entity.Department;
import com.situ.personal.entity.PageInfo;
import com.situ.personal.entity.User;
import com.situ.personal.util.MD5Util;
import com.situ.personal.vo.DepartmentCountVO;

@WebServlet("/user")
public class UserServlet extends HttpServlet{
	private UserDao userdao = new UserDao();
	private DepartmentDao departmentDao = new DepartmentDao();
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("123321");
		String method = req.getParameter("method");
		switch (method) {
		case "selectAll":
			selectAll(req,resp);
			break;
		case "deleteById":
			deleteById(req,resp);
			break;
		case "selectDepartment":
			selectDepartment(req,resp);
			break;
		case "insert":
			insert(req,resp);
			break;
		case "updateById":
			updateById(req,resp);
			break;
		case "selectById":
			selectById(req,resp);
			break;
		case "selectByPage":
			selectByPage(req,resp);
			break;
		case "selectCount":
			selectCount(req,resp);
			break;

		default:
			break;
		}
	}

	private void selectCount(HttpServletRequest req, HttpServletResponse resp) {
		System.out.println("UserServlet.selectCount()");
		ArrayList<DepartmentCountVO> list = userdao.selectCount();
	}

	private void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("UserServlet.selectByPage()");
		String pageNoString = req.getParameter("pageNo");
		String pageSizeString = req.getParameter("pageSize");
		
		if (pageNoString == null || "".equals(pageNoString)) {
			pageNoString = "1";
		}
		if (pageSizeString == null || "".equals(pageSizeString)) {
			pageSizeString = "5";
		}
		
		int pageNo = Integer.parseInt(pageNoString);
		int pageSize = Integer.parseInt(pageSizeString);
			
		ArrayList<User> list = userdao.selectByPage(pageNo,pageSize);
		int totalPage = userdao.selectPageCount(req,resp,pageNo,pageSize);
		PageInfo<User> pageInfo = new PageInfo<User>();
		pageInfo.setList(list);
		pageInfo.setTotalPage(totalPage);
		pageInfo.setPageNo(pageNo);
		pageInfo.setPageSize(pageSize);
		req.setAttribute("pageInfo", pageInfo);
		req.getRequestDispatcher("/user_list.jsp").forward(req, resp);
	}

	private void selectById(HttpServletRequest req, HttpServletResponse resp) {
		System.out.println("UserServlet.selectById()");
		String id = req.getParameter("id");
		userdao.selectById(Integer.parseInt(id), resp);
	}

	private void updateById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		System.out.println("UserServlet.updateById()");
		String id = req.getParameter("id");
		String name = req.getParameter("name");
		String passwordBefore = req.getParameter("password");
		String password = MD5Util.MD5Encode(passwordBefore);
		String email = req.getParameter("email");
		String department_id = req.getParameter("department_id");
		String age = req.getParameter("age");
		String pageNo = req.getParameter("pageNo");
		int count = UserDao.updateById(Integer.parseInt(id),name,password,email,Integer.parseInt(department_id),Integer.parseInt(age));
		
		resp.sendRedirect(req.getContextPath() + "/user?method=selectByPage&pageNo=" + pageNo);
	}

	private void insert(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		System.out.println("UserServlet.insert()");
		
		String name = req.getParameter("name");
		String password = req.getParameter("password");
		String email = req.getParameter("email");
		String department_id = req.getParameter("department_id");
		String age = req.getParameter("age");
		String pageNo = req.getParameter("pageNo");
		
		String passwordMD5 = passwordToMD5(password);
		System.out.println(passwordMD5);
		int count = userdao.insert(name,passwordMD5,email,Integer.parseInt(department_id),Integer.parseInt(age));
		resp.sendRedirect(req.getContextPath() + "/user?method=selectByPage&pageNo=" + pageNo);
	}

	private void selectDepartment(HttpServletRequest req, HttpServletResponse resp) {
		System.out.println("UserServlet.selectDepartment()");
		departmentDao.selectDepartment(resp);
	}

	private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		System.out.println("UserServlet.deleteById()");
		String id = req.getParameter("id");
		String pageNo = req.getParameter("pageNo");
		int count = userdao.deleteById(Integer.parseInt(id));
		resp.sendRedirect(req.getContextPath() + "/user?method=selectByPage&pageNo=" + pageNo);
	}

	private void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("UserServlet.selectAll()");
		ArrayList<User> list = userdao.selectAll();
		
		for(User user : list) {
			System.out.println(user);
		}
		
		req.setAttribute("list", list);
		req.getRequestDispatcher("user_list.jsp").forward(req, resp);
	}
	
	public static String passwordToMD5(String passWord) {
		String result = MD5Util.MD5Encode(passWord);
		
		return result;
	}
}
